package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem;

import android.content.Context;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.dal.ProductInfoDAL;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.database.DBHelper;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;

@RunWith(AndroidJUnit4.class)
public class ProductInfoDALTest {
    private Context context;
    private ProductInfoDAL productInfoDAL;

    @Before
    public void setUp() {
        context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        DBHelper dbHelper = new DBHelper(context, true);
        productInfoDAL = new ProductInfoDAL(context);
    }

    @Test
    public void testGetProductsWithState0() {
        Product product1 = new Product("A", "a", new byte[]{'a'}, "wasd", 10.0, "WASD", 0);
        Product product2 = new Product("B", "b", new byte[]{'a'}, "wasd", 10.0, "WASD", 0);
        productInfoDAL.addOneProduct(product1);
        productInfoDAL.addOneProduct(product2);
        List<Product> result = productInfoDAL.getAllProductsByState(0, -1);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    }

    @Test
    public void testSearchProduct() {
        Product product1 = new Product("dwasdwa", "dwasdwa", new byte[]{'a'}, "123", 10.0, "WASD", 0);
        Product product2 = new Product("dwasdwaaa", "dwasdwaaa", new byte[]{'a'}, "321", 10.0, "WASD", 0);
        Product product3 = new Product("dwdwdwdw", "dwdwdwdw", new byte[]{'a'}, "fesdff", 10.0, "WASD", 0);
        productInfoDAL.addOneProduct(product1);
        productInfoDAL.addOneProduct(product2);
        productInfoDAL.addOneProduct(product3);

        List<Product> result = productInfoDAL.searchProduct("dw");
        Log.d("TEST//", result.toString());
    }

    @Test
    public void testPurchase() {
        Product product1 = new Product("dwasdwa", "dwasdwa", new byte[]{'a'}, "123", 10.0, "WASD", 0);
        Product product2 = new Product("dwasdwaaa", "dwasdwaaa", new byte[]{'a'}, "321", 10.0, "WASD", 0);
        productInfoDAL.addOneProduct(product1);
        productInfoDAL.addOneProduct(product2);

        productInfoDAL.updateOneProductState("dwasdwa", 2);
        productInfoDAL.updateOneProductState("dwasdwaaa", 4);

        Product p1 = productInfoDAL.getOneProductInfoById("dwasdwa", -1);
        Product p2 = productInfoDAL.getOneProductInfoById("dwasdwaaa", -1);

        Log.d("TEST//", p1.toString());
        Log.d("TEST//", p2.toString());
    }
}
